// pages/money/card/index.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        userInfo: null,
        show: false,
        userName: '',
        bankName: '',
        bankCard: '',
        bankPhone: '',
        bankList:[
            {logo: '/images/bank/zg.png',name:'中国银行'},
            {logo: '/images/bank/gs.png',name:'工商银行'},
            {logo: '/images/bank/js.png',name:'建设银行'},
            {logo: '/images/bank/ny.png',name:'农业银行'},
            {logo: '/images/bank/zs.png',name:'招商银行'},
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = wx.getStorageSync('userInfo');
        if(userInfo){
            this.setData({
                userInfo: userInfo
            })
            this.getMyBankCard();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //持卡人提示
    showNameTips: function () {
        wx.showModal({
            title: '商户说明',
            content: '为了资金安全，需与法人姓名认证一致。',
            showCancel: false,
            confirmColor: '#378bfd',
            confirmText: '我知道了'
        })
    },
    //提示预留手机号
    showPhoneTips: function(){
        wx.showModal({
            title: '手机号说明',
            content: '银行预留手机号是您在办理该银行卡时所填写的手机号。若不知道预留手机号或预留手机号已停用等，请与银行客服联系处理。',
            showCancel: false,
            confirmColor: '#378bfd',
            confirmText: '我知道了'
        })
    },
    //打开银行弹窗
    openPop: function(){
        this.setData({show: true});
    },
    //关于弹框
    closePop: function(){
        this.setData({show: false});
    },
    //选择银行
    selectedBank: function(e){
        let {bankList} = this.data;
        let index = e.currentTarget.dataset.index;
        this.setData({
            bankName: bankList[index].name
        })
        this.closePop();
    },
    //获取绑卡信息
    getMyBankCard: function(){
        wx.$http({
            load: true,
            site: 'phpSite',
            method: 'get',
            url: '/business/bank-info',
            data: {},
        }).then((res) => {
            let data = res.data;
            if(data){
                this.setData({
                    id: data.id,
                    bankName: data.bank_name,
                    bankCard: data.card,
                    bankPhone: data.phone,
                    userName: data.realname,
                })
            }
        })
    },
    //绑定银行卡
    saveBank: function(){
        let { id,userName, bankName, bankCard, bankPhone } = this.data;
        if(!bankCard){
            wx.showToast({
                title: '请输入银行卡号',
                icon: 'none'
            })
            return;
        }else if(!wx.$checkReg('card', bankCard)){
            wx.showToast({
                title: '银行卡号格式错误',
                icon: 'none'
            })
            return;
        }
        if(!bankName){
            wx.showToast({
                title: '请选择开户行',
                icon: 'none'
            })
            return;
        }
        if(!bankPhone){
            wx.showToast({
                title: '请输入银行预留手机号',
                icon: 'none'
            })
            return;
        }else if(!wx.$checkReg('card', bankPhone)){
            wx.showToast({
                title: '手机号号格式错误',
                icon: 'none'
            })
            return;
        }
        wx.$http({
            load: true,
            url: '/business/save-bank',
            data: {
                realname: userName,
                bank_name: bankName,
                card: bankCard,
                phone: bankPhone
            },
        }).then((res) => {
            wx.showToast({
                title: '绑卡成功！',
            })
            setTimeout(()=>{
                wx.$utils.goBack();
            },1000)
        })
    }
})